package filters;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import beanUtils.BeanUtility;
import beans.BankDetailsBean;

;

/**
 * Servlet Filter implementation class bankdetails_filter
 */
public class bankdetails_filter implements Filter {
	int count = 0;

	/**
	 * Default constructor.
	 */
	public bankdetails_filter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("in bd filter");
		Map<String, String> errormap = new HashMap<String, String>();
		BankDetailsBean bdb = new BankDetailsBean();
		BeanUtility.populateBean(bdb, (HttpServletRequest) request);
		System.out.println(bdb.toString());
		try {
			validate(bdb, errormap);
			if (count > 0) {
				count = 0;
				request.setAttribute("errormap", errormap);
				RequestDispatcher rd = request
						.getRequestDispatcher("registration.jsp");
				rd.forward(request, response);
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	private void validate(BankDetailsBean bdb, Map<String, String> errormap)
			throws Exception {
		System.out.println("in bd validate function");
		branch(bdb.getBranch(), errormap);
		accountno(bdb.getAccountno(), errormap);


	}

	private void branch(String branch, Map<String, String> errormap) {
		if (branch.equals("")) {
			errormap.put("branch", "enter branch name");
			count++;

		} else {
			String regex = "[a-zA-Z][a-zA-Z]*";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(branch.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In branch function : " + branch);
				errormap.put("branch", "invalid branch name");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}
	}
	
	private void accountno(String accountno, Map<String, String> errormap) {
		if (accountno.equals("")) {
			errormap.put("accountno", "enter accountno name");
			count++;

		} else {
			String regex = "[0-9]{17}";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(accountno.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In accountno function : " + accountno);
				errormap.put("accountno", "invalid accountno name");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}
	}


	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
